Skip to content
  • Watch

    Notifications

    Get push notifications on iOS or Android.

Elixir client libraries for accessing Google APIs.

Watch

Notifications

Get push notifications on iOS or Android.
Open in github.dev
master
Switch branches/tags
Go to file
Add file
Code

Latest commit

This PR was generated using Autosynth. rainbow


<details><summary>Log from Synthtool</summary>

```
2021-09-15 06:51:37,872 synthtool [DEBUG] > Executing /home/kbuilder/.cache/synthtool/elixir-google-api/synth.py.
On branch autosynth-spanner
nothing to commit, working tree clean
2021-09-15 06:51:39,791 synthtool [DEBUG] > Running: docker run --rm -v/tmpfs/tmp/tmpe6fepawx/repo:/workspace -v/var/run/docker.sock:/var/run/docker.sock -e USER_GROUP=1000:1000 -w /workspace gcr.io/cloud-devrel-public-resources/elixir19 scripts/generate_client.sh Spanner
DEBUG:synthtool:Running: docker run --rm -v/tmpfs/tmp/tmpe6fepawx/repo:/workspace -v/var/run/docker.sock:/var/run/docker.sock -e USER_GROUP=1000:1000 -w /workspace gcr.io/cloud-devrel-public-resources/elixir19 scripts/generate_client.sh Spanner
/workspace /workspace
�[33mThe mix.lock file was generated with a newer version of Hex. Update your client by running `mix local.hex` to avoid losing data.�[0m
Resolving Hex dependencies...
Dependency resolution completed:
Unchanged:
  certifi 2.5.1
  google_api_discovery 0.7.0
  google_gax 0.3.2
  hackney 1.15.2
  idna 6.0.0
  jason 1.2.1
  metrics 1.0.1
  mime 1.3.1
  mimerl 1.2.0
  oauth2 0.9.4
  parse_trans 3.3.0
  poison 3.1.0
  ssl_verify_fun 1.1.5
  temp 0.4.7
  tesla 1.3.3
  unicode_util_compat 0.4.1
* Getting google_api_discovery (Hex package)
* Getting tesla (Hex package)
* Getting oauth2 (Hex package)
* Getting temp (Hex package)
* Getting jason (Hex package)
* Getting poison (Hex package)
* Getting hackney (Hex package)
* Getting certifi (Hex package)
* Getting idna (Hex package)
* Getting metrics (Hex package)
* Getting mimerl (Hex package)
* Getting ssl_verify_fun (Hex package)
* Getting unicode_util_compat (Hex package)
* Getting parse_trans (Hex package)
* Getting mime (Hex package)
* Getting google_gax (Hex package)
�[33mThe mix.lock file was generated with a newer version of Hex. Update your client by running `mix local.hex` to avoid losing data.�[0m
==> temp
Compiling 3 files (.ex)
Generated temp app
===> Compiling parse_trans
===> Compiling mimerl
===> Compiling metrics
===> Compiling unicode_util_compat
===> Compiling idna
==> jason
Compiling 8 files (.ex)
Generated jason app
warning: String.strip/1 is deprecated. Use String.trim/1 instead
  /workspace/deps/poison/mix.exs:4

==> poison
Compiling 4 files (.ex)
warning: Integer.to_char_list/2 is deprecated. Use Integer.to_charlist/2 instead
  lib/poison/encoder.ex:173

Generated poison app
==> ssl_verify_fun
Compiling 7 files (.erl)
Generated ssl_verify_fun app
===> Compiling certifi
===> Compiling hackney
==> oauth2
Compiling 13 files (.ex)
Generated oauth2 app
==> mime
Compiling 2 files (.ex)
Generated mime app
==> tesla
Compiling 26 files (.ex)
Generated tesla app
==> google_gax
Compiling 5 files (.ex)
Generated google_gax app
==> google_api_discovery
Compiling 21 files (.ex)
Generated google_api_discovery app
==> google_apis
Compiling 28 files (.ex)
Generated google_apis app

13:52:13.973 [info]  FETCHING: https://spanner.googleapis.com/$discovery/GOOGLE_REST_SIMPLE_URI?version=v1

13:52:14.100 [info]  FETCHING: https://spanner.googleapis.com/$discovery/rest?version=v1

13:52:14.141 [info]  FOUND: https://spanner.googleapis.com/$discovery/rest?version=v1
Revision check: old=20210825, new=20210903, generating=true
Creating leading directories
Writing Backup to clients/spanner/lib/google_api/spanner/v1/model/backup.ex.
Writing BackupInfo to clients/spanner/lib/google_api/spanner/v1/model/backup_info.ex.
Writing BatchCreateSessionsRequest to clients/spanner/lib/google_api/spanner/v1/model/batch_create_sessions_request.ex.
Writing BatchCreateSessionsResponse to clients/spanner/lib/google_api/spanner/v1/model/batch_create_sessions_response.ex.
Writing BeginTransactionRequest to clients/spanner/lib/google_api/spanner/v1/model/begin_transaction_request.ex.
Writing Binding to clients/spanner/lib/google_api/spanner/v1/model/binding.ex.
Writing ChildLink to clients/spanner/lib/google_api/spanner/v1/model/child_link.ex.
Writing CommitRequest to clients/spanner/lib/google_api/spanner/v1/model/commit_request.ex.
Writing CommitResponse to clients/spanner/lib/google_api/spanner/v1/model/commit_response.ex.
Writing CommitStats to clients/spanner/lib/google_api/spanner/v1/model/commit_stats.ex.
Writing ContextValue to clients/spanner/lib/google_api/spanner/v1/model/context_value.ex.
Writing CreateBackupMetadata to clients/spanner/lib/google_api/spanner/v1/model/create_backup_metadata.ex.
Writing CreateDatabaseMetadata to clients/spanner/lib/google_api/spanner/v1/model/create_database_metadata.ex.
Writing CreateDatabaseRequest to clients/spanner/lib/google_api/spanner/v1/model/create_database_request.ex.
Writing CreateInstanceMetadata to clients/spanner/lib/google_api/spanner/v1/model/create_instance_metadata.ex.
Writing CreateInstanceRequest to clients/spanner/lib/google_api/spanner/v1/model/create_instance_request.ex.
Writing CreateSessionRequest to clients/spanner/lib/google_api/spanner/v1/model/create_session_request.ex.
Writing Database to clients/spanner/lib/google_api/spanner/v1/model/database.ex.
Writing Delete to clients/spanner/lib/google_api/spanner/v1/model/delete.ex.
Writing DerivedMetric to clients/spanner/lib/google_api/spanner/v1/model/derived_metric.ex.
Writing DiagnosticMessage to clients/spanner/lib/google_api/spanner/v1/model/diagnostic_message.ex.
Writing Empty to clients/spanner/lib/google_api/spanner/v1/model/empty.ex.
Writing EncryptionConfig to clients/spanner/lib/google_api/spanner/v1/model/encryption_config.ex.
Writing EncryptionInfo to clients/spanner/lib/google_api/spanner/v1/model/encryption_info.ex.
Writing ExecuteBatchDmlRequest to clients/spanner/lib/google_api/spanner/v1/model/execute_batch_dml_request.ex.
Writing ExecuteBatchDmlResponse to clients/spanner/lib/google_api/spanner/v1/model/execute_batch_dml_response.ex.
Writing ExecuteSqlRequest to clients/spanner/lib/google_api/spanner/v1/model/execute_sql_request.ex.
Writing Expr to clients/spanner/lib/google_api/spanner/v1/model/expr.ex.
Writing Field to clients/spanner/lib/google_api/spanner/v1/model/field.ex.
Writing GetDatabaseDdlResponse to clients/spanner/lib/google_api/spanner/v1/model/get_database_ddl_response.ex.
Writing GetIamPolicyRequest to clients/spanner/lib/google_api/spanner/v1/model/get_iam_policy_request.ex.
Writing GetPolicyOptions to clients/spanner/lib/google_api/spanner/v1/model/get_policy_options.ex.
Writing IndexedHotKey to clients/spanner/lib/google_api/spanner/v1/model/indexed_hot_key.ex.
Writing IndexedKeyRangeInfos to clients/spanner/lib/google_api/spanner/v1/model/indexed_key_range_infos.ex.
Writing Instance to clients/spanner/lib/google_api/spanner/v1/model/instance.ex.
Writing InstanceConfig to clients/spanner/lib/google_api/spanner/v1/model/instance_config.ex.
Writing KeyRange to clients/spanner/lib/google_api/spanner/v1/model/key_range.ex.
Writing KeyRangeInfo to clients/spanner/lib/google_api/spanner/v1/model/key_range_info.ex.
Writing KeyRangeInfos to clients/spanner/lib/google_api/spanner/v1/model/key_range_infos.ex.
Writing KeySet to clients/spanner/lib/google_api/spanner/v1/model/key_set.ex.
Writing ListBackupOperationsResponse to clients/spanner/lib/google_api/spanner/v1/model/list_backup_operations_response.ex.
Writing ListBackupsResponse to clients/spanner/lib/google_api/spanner/v1/model/list_backups_response.ex.
Writing ListDatabaseOperationsResponse to clients/spanner/lib/google_api/spanner/v1/model/list_database_operations_response.ex.
Writing ListDatabasesResponse to clients/spanner/lib/google_api/spanner/v1/model/list_databases_response.ex.
Writing ListInstanceConfigsResponse to clients/spanner/lib/google_api/spanner/v1/model/list_instance_configs_response.ex.
Writing ListInstancesResponse to clients/spanner/lib/google_api/spanner/v1/model/list_instances_response.ex.
Writing ListOperationsResponse to clients/spanner/lib/google_api/spanner/v1/model/list_operations_response.ex.
Writing ListScansResponse to clients/spanner/lib/google_api/spanner/v1/model/list_scans_response.ex.
Writing ListSessionsResponse to clients/spanner/lib/google_api/spanner/v1/model/list_sessions_response.ex.
Writing LocalizedString to clients/spanner/lib/google_api/spanner/v1/model/localized_string.ex.
Writing Metric to clients/spanner/lib/google_api/spanner/v1/model/metric.ex.
Writing MetricMatrix to clients/spanner/lib/google_api/spanner/v1/model/metric_matrix.ex.
Writing MetricMatrixRow to clients/spanner/lib/google_api/spanner/v1/model/metric_matrix_row.ex.
Writing Mutation to clients/spanner/lib/google_api/spanner/v1/model/mutation.ex.
Writing Operation to clients/spanner/lib/google_api/spanner/v1/model/operation.ex.
Writing OperationProgress to clients/spanner/lib/google_api/spanner/v1/model/operation_progress.ex.
Writing OptimizeRestoredDatabaseMetadata to clients/spanner/lib/google_api/spanner/v1/model/optimize_restored_database_metadata.ex.
Writing PartialResultSet to clients/spanner/lib/google_api/spanner/v1/model/partial_result_set.ex.
Writing Partition to clients/spanner/lib/google_api/spanner/v1/model/partition.ex.
Writing PartitionOptions to clients/spanner/lib/google_api/spanner/v1/model/partition_options.ex.
Writing PartitionQueryRequest to clients/spanner/lib/google_api/spanner/v1/model/partition_query_request.ex.
Writing PartitionReadRequest to clients/spanner/lib/google_api/spanner/v1/model/partition_read_request.ex.
Writing PartitionResponse to clients/spanner/lib/google_api/spanner/v1/model/partition_response.ex.
Writing PartitionedDml to clients/spanner/lib/google_api/spanner/v1/model/partitioned_dml.ex.
Writing PlanNode to clients/spanner/lib/google_api/spanner/v1/model/plan_node.ex.
Writing Policy to clients/spanner/lib/google_api/spanner/v1/model/policy.ex.
Writing PrefixNode to clients/spanner/lib/google_api/spanner/v1/model/prefix_node.ex.
Writing QueryOptions to clients/spanner/lib/google_api/spanner/v1/model/query_options.ex.
Writing QueryPlan to clients/spanner/lib/google_api/spanner/v1/model/query_plan.ex.
Writing ReadOnly to clients/spanner/lib/google_api/spanner/v1/model/read_only.ex.
Writing ReadRequest to clients/spanner/lib/google_api/spanner/v1/model/read_request.ex.
Writing ReadWrite to clients/spanner/lib/google_api/spanner/v1/model/read_write.ex.
Writing ReplicaInfo to clients/spanner/lib/google_api/spanner/v1/model/replica_info.ex.
Writing RequestOptions to clients/spanner/lib/google_api/spanner/v1/model/request_options.ex.
Writing RestoreDatabaseEncryptionConfig to clients/spanner/lib/google_api/spanner/v1/model/restore_database_encryption_config.ex.
Writing RestoreDatabaseMetadata to clients/spanner/lib/google_api/spanner/v1/model/restore_database_metadata.ex.
Writing RestoreDatabaseRequest to clients/spanner/lib/google_api/spanner/v1/model/restore_database_request.ex.
Writing RestoreInfo to clients/spanner/lib/google_api/spanner/v1/model/restore_info.ex.
Writing ResultSet to clients/spanner/lib/google_api/spanner/v1/model/result_set.ex.
Writing ResultSetMetadata to clients/spanner/lib/google_api/spanner/v1/model/result_set_metadata.ex.
Writing ResultSetStats to clients/spanner/lib/google_api/spanner/v1/model/result_set_stats.ex.
Writing RollbackRequest to clients/spanner/lib/google_api/spanner/v1/model/rollback_request.ex.
Writing Scan to clients/spanner/lib/google_api/spanner/v1/model/scan.ex.
Writing ScanData to clients/spanner/lib/google_api/spanner/v1/model/scan_data.ex.
Writing Session to clients/spanner/lib/google_api/spanner/v1/model/session.ex.
Writing SetIamPolicyRequest to clients/spanner/lib/google_api/spanner/v1/model/set_iam_policy_request.ex.
Writing ShortRepresentation to clients/spanner/lib/google_api/spanner/v1/model/short_representation.ex.
Writing Statement to clients/spanner/lib/google_api/spanner/v1/model/statement.ex.
Writing Status to clients/spanner/lib/google_api/spanner/v1/model/status.ex.
Writing StructType to clients/spanner/lib/google_api/spanner/v1/model/struct_type.ex.
Writing TestIamPermissionsRequest to clients/spanner/lib/google_api/spanner/v1/model/test_iam_permissions_request.ex.
Writing TestIamPermissionsResponse to clients/spanner/lib/google_api/spanner/v1/model/test_iam_permissions_response.ex.
Writing Transaction to clients/spanner/lib/google_api/spanner/v1/model/transaction.ex.
Writing TransactionOptions to clients/spanner/lib/google_api/spanner/v1/model/transaction_options.ex.
Writing TransactionSelector to clients/spanner/lib/google_api/spanner/v1/model/transaction_selector.ex.
Writing Type to clients/spanner/lib/google_api/spanner/v1/model/type.ex.
Writing UpdateDatabaseDdlMetadata to clients/spanner/lib/google_api/spanner/v1/model/update_database_ddl_metadata.ex.
Writing UpdateDatabaseDdlRequest to clients/spanner/lib/google_api/spanner/v1/model/update_database_ddl_request.ex.
Writing UpdateInstanceMetadata to clients/spanner/lib/google_api/spanner/v1/model/update_instance_metadata.ex.
Writing UpdateInstanceRequest to clients/spanner/lib/google_api/spanner/v1/model/update_instance_request.ex.
Writing VisualizationData to clients/spanner/lib/google_api/spanner/v1/model/visualization_data.ex.
Writing Write to clients/spanner/lib/google_api/spanner/v1/model/write.ex.
Writing Projects to clients/spanner/lib/google_api/spanner/v1/api/projects.ex.
Writing Scans to clients/spanner/lib/google_api/spanner/v1/api/scans.ex.
Writing connection.ex.
Writing metadata.ex.
Writing mix.exs
Writing README.md
Writing LICENSE
Writing .gitignore
Writing config/config.exs
Writing test/test_helper.exs
�[33mThe mix.lock file was generated with a newer version of Hex. Update your client by running `mix local.hex` to avoid losing data.�[0m

13:52:16.124 [info]  Bumping patch
fixing file permissions
2021-09-15 06:52:19,112 synthtool [DEBUG] > Wrote metadata to clients/spanner/synth.metadata.
DEBUG:synthtool:Wrote metadata to clients/spanner/synth.metadata.

```
</details>

Full log will be available here:
https://source.cloud.google.com/results/invocations/d8b9643d-afe4-4100-9f83-3f7293949d37/targets

- [ ] To automatically regenerate this PR, check this box. (May take up to 24 hours.)
69b7e3f

Git stats

Files

Permalink
Failed to load latest commit information.

GoogleApis

This repository contains all the client libraries to interact with Google APIs. These client libraries are created under clients/ and each should contain its own README.

The main folder contains the code necessary to generate these client libraries.

NOTE: These generated clients are under development and should be considered experimental!

Usage

Installation

All available Google API clients can be found on hex.pm. Add a client to your project's mix.exs under deps:

defmodule YourApplication.Mixfile do
  use Mix.Project
  #...

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
      {:google_api_storage, "~> 0.19.0"},
      {:goth, "~> 1.2.0"}
    ]
  end
end

Note the goth package, which handles Google Authentication, is also required.

Next, run mix deps.get to pull down the dependencies:

$ mix deps.get

Now you can make an API call by obtaining an access token and using the generated modules.

Obtaining an Access Token

Service Accounts

Authentication is typically done through Application Default Credentials which means you do not have to change the code to authenticate as long as your environment has credentials. Start by creating a Service Account key file. This file can be used to authenticate to Google Cloud Platform services from any environment. To use the file, set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path to the key file, for example:

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json

If you are deploying to App Engine, Compute Engine, or Container Engine, your credentials will be available by default.

OAuth 2.0

Many APIs (like Drive, Gmail, and YouTube) require you to use OAuth 2.0 to authorize requests on behalf of an authenticated user. For an example using the oauth2-library, see the auth sample.

We've also provided a mix task to fetch a token for testing. The following command requests a token for the Drive full access scope:

$ export GOOGLE_CLIENT_ID=[YOUR-OAUTH-CLIENT-ID]
$ export GOOGLE_CLIENT_SECRET=[YOUR-OAUTH-CLIENT-SECRET]
$ mix google_apis.auth https://www.googleapis.com/auth/drive
Open the following link in your brower:
https://accounts.google.com/o/oauth2/auth?[some-long-url]
Enter verification code:

Once you've logged in and authorized the application, copy the code param from the web browser's url and paste into the console. The script will then fetch your OAuth access token.

Token: [your-oauth-token]

You can then use this token for your testing:

connection = GoogleApi.Drive.V3.Connection.new("your-oauth-token")
{:ok, file_list} = GoogleApi.Drive.V3.Api.Files.drive_files_list(conn)

Making a Request

# Obtain an access token using goth
{:ok, token} = Goth.Token.for_scope("https://www.googleapis.com/auth/cloud-platform")
conn = GoogleApi.Storage.V1.Connection.new(token.token)

# Call the Storage V1 API (for example) to list buckets
{:ok, response} = GoogleApi.Storage.V1.Api.Buckets.storage_buckets_list(conn, project_id)

# Print the response
Enum.each(response.items, &IO.puts(&1.id))

What's Next?

Take a look at our elixir-samples repository repository for examples of calling individual APIs and a getting started tutorial app.

Generating Clients

Setup

  1. Install nodejs if not already installed.
  2. Install nodejs dependencies:
$> npm install
  1. Install elixir dependencies:
$> mix deps.get

This project provides 4 mix tasks to componentize the build process:

  1. mix google_apis.discover - Select which APIs to build
  2. mix google_apis.fetch - Download the selected API specifications in Google discovery format
  3. mix google_apis.convert - Convert the selected API specifications from Google discovery format to OpenApi v2 (formerly known as Swagger)
  4. mix google_apis.build - Generate API clients

Selecting APIs

The mix google_apis.discover task queries Google's API discovery directory. The contents of this file are downloaded to a staging file (api-candidate.json) under the config directory.

You can change the name of the file by providing a filename argument to the mix task:

$> mix google_apis.discover foo.json

Note that this task is not one that should be run often, as the config/api.json is considered configuration regarding which APIs to generate.

Fetching API Specifications

The mix google_apis.fetch task iterates through the list of API specifications in the config/api.json file and downloads the specification to the specifications/gdd folder with the format of <name>-<version>.json.

You can limit which APIs to fetch by providing an API name argument to the mix task:

$> mix google_apis.fetch CloudTrace

Converting API Specifications

The next step is to convert the API specifications from Google's discovery format to OpenApi format. The mix google_apis.convert task iterates through the list of API specifications in the config/api.json file and converts each found Google discovery specification to an equivalent* OpenApi version.

You can configure the converter by modifying the config/config.exs setting:

config :google_apis, spec_converter: <some converter implementation>

The default converter uses the node package api-spec-converter. You can also limit which APIs to convert by providing an API name argument to the mix task:

$> mix google_apis.convert CloudTrace

Building API Clients

The mix google_apis.build task iterates through the list of API specifications in the config/api.json file and generates an Elixir client library in the clients folder.

You can configure the converter by modifying the config/config.exs setting:

config :google_apis, client_generator: GoogleApis.Generator.SwaggerCli

The default generator uses Docker and an image based off the swagger-codegen project. You can further configure this converter by modifying the config/config.exs setting:

config :google_apis, swagger_cli_image: "swagger-cli"

You can also limit which APIs to generate by providing an API name argument to the mix task:

$> mix google_apis.generate CloudTrace

Contributing

Contributions to this library are always welcome and highly encouraged.

See CONTRIBUTING for more information on how to get started.

License

Apache 2.0 - See LICENSE for more information.

Disclaimer

This is not an officially supported Google product.

About

Elixir client libraries for accessing Google APIs.

Resources

License

Releases

No releases published

Packages

No packages published

Languages